<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use think\Log;

class Metadata extends AdminBase
{


    protected function _initialize()
    {
        parent::_initialize();
        if ($this->request->isGet()) {
            $this->assign('metadataType', model('metadataType')->order('id asc')->select());
        }
    }

    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['name'])) {
            $where['name'] = ['like', "%" . $param['name'] . "%"];
        }
        if (isset($param['meta_type'])) {
            $where['meta_type'] = $param['meta_type'];
        }
        $list = db('resource_metadata')->alias('rm')
            ->join('resource_metadata_type rmt', 'rm.meta_type = rmt.id', 'left')
            ->field('rm.id as id, rm.name, rmt.name as typeName, rm.line_count, rm.size, rm.create_time, rm.last_time')
            ->order('create_time desc')
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('index', ['list' => $list]);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['create_time'] = date('Y-m-d H:i:s');
            $param['last_time'] = date('Y-m-d H:i:s');
            if ($this->insert('metadata', $param, false) === true) {
                insert_admin_log('添加了元数据');
                $this->success('添加成功', url('admin/metadata/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', ['metadata' => model('metadata')->order('id asc')->select()]);
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['last_time'] = date('Y-m-d H:i:s');
            if ($this->update('metadata', $param, false,input('_verify', true)) === true) {
                insert_admin_log('修改了元数据');
                $this->success('修改成功', url('admin/metadata/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', [
            'data'     => model('metadata')->where('id', input('id'))->find(),
            'metadata' => model('metadata')->order('id asc')->select(),
        ]);
    }

    public function importData()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $meta = model('metadata')->where('id', '=', $param['id'])->find();
            if ($meta == null) {
                $this->error('请选择要导入的数据');
            }
            // 获取参数中的excel
            $file = $this->request->file('file');
            // 获取导入数据
            $excel_array = import_excel($file);
            if ($excel_array == null || sizeof($excel_array) == 0) {
                $this->error('导入失败，请确认类型或者文件是否有效');
            }
            $metadataLog = array();
            // 中文论文
            if ($meta['meta_type'] == 1) {
                $metadataLog = $this->getDbDataByType1($excel_array, $meta['meta_type']);
            }
            // 项目纵向
            elseif ($meta['meta_type'] == 2) {
                $metadataLog = $this->getDbDataByType2($excel_array, $meta['meta_type']);
            }
            // 项目横向
            elseif ($meta['meta_type'] == 3) {
                $metadataLog = $this->getDbDataByType3($excel_array, $meta['meta_type']);
            }
            // 专利
            elseif ($meta['meta_type'] == 4) {
                $metadataLog = $this->getDbDataByType4($excel_array, $meta['meta_type']);
            }
            // SCI
            elseif ($meta['meta_type'] == 5) {
                $metadataLog = $this->getDbDataByType5($excel_array, $meta['meta_type']);
            }
            // EI
            elseif ($meta['meta_type'] == 6) {
                $metadataLog = $this->getDbDataByType5($excel_array, $meta['meta_type']);
            }
            // 图书
            elseif ($meta['meta_type'] == 7) {
                $metadataLog = $this->getDbDataByType5($excel_array, $meta['meta_type']);
            }
            // 成果奖励
            elseif ($meta['meta_type'] == 8) {
                $metadataLog = $this->getDbDataByType8($excel_array, $meta['meta_type']);
            }
            // 其他
            else {
                $this->success('导入类型其他', url('admin/metadata/index'));
            }
            if (!empty($metadataLog)) {
                $this->success('导入成功', '', $metadataLog);
            } else {
                $this->error('导入失败', url('admin/metadata/index'));
            }
        }
        return $this->fetch('importData', [
            'data'     => model('metadata')->where('id', input('id'))->find(),
            'metadata' => model('metadata')->order('id asc')->select(),
        ]);
    }

    public function sureImport() {
        $param = $this->request->param();
        // 读取临时表数据
        $dbData = model('collectionTemporary')->where('batch_no', '=', $param['batchNo'])->select()->toArray();
        if (empty($dbData)) {
            $this->error('该批次没有数据');
        }
        $collection = array();
        foreach ($dbData as $k=>$v) {
            array_splice($v, 1, 1);
            $collection[$k] = $v;
        }

        Log::write($collection);


        //自动认领
        foreach ($collection as $k=>$v) {
            //db::name('user')->where('name','=',$v["creator"]);
            if(!empty($v['creator']) && strlen($v['creator']) > 0){
                $userCount = db::name('user')->field('count(1) count')->where('user_name','=',$v['creator'])->select();
                $userResource = [];
                if(!empty($userCount) && $userCount[0]['count'] == 1){
                    /*$userName = '';
                    if($v['resourceCode'] == 'A' || $v['resourceCode'] == 'O' || $v['resourceCode'] == 'P'){
                        $userName = $v[''];
                    }*/
                    $userInfo = db::name('user')->where('user_name','=',$v['creator'])->find();
                    $userResource['user_id'] = $userInfo['id'];
                    $userResource['resource_id'] = $v['id'];
                    $userResource['create_time'] = date('Y-m-d H:i:s');
                    $userResource['resource_code'] = $v['resourceCode'];
                    $userResource['year'] = $v['year'];
                    $userResource['status'] = 1;
                    db::name('user_resource')->insert($userResource,true);
                    db::name('local_resource_collection')->insert($v,true);
                }
            }
        }


        // 批量覆盖插入数据
        db::name('resource_collection')->insertAll($collection, true, 1000);

        // 修改记录表数据
        $meta = model('metadata')->where('id', '=', $param['id'])->find();
        if ($meta != null) {
            $metadata['id'] = $param['id'];
            $metadata['line_count'] = $meta['line_count'] + sizeof($collection);
            $metadata['last_time'] = date('Y-m-d H:i:s');
            $this->update('metadata', $metadata, false);
        }
        // 修改日志表记录
        $log = model('metadataLog')->where('batch_no', '=', $param['batchNo'])->find();
        if ($log != null) {
            $metadataLog['id'] = $log['id'];
            $metadataLog['opt_type'] = 1;
            $metadataLog['update_time'] = date('Y-m-d H:i:s');
            $this->update('metadataLog', $metadataLog, false);
        }
        $this->success('导入成功', url('admin/metadata/index'));
    }

    public function skipImport() {
        $param = $this->request->param();

        // 修改日志表记录
        $log = model('metadataLog')->where('batch_no', '=', $param['batchNo'])->find();
        if ($log != null) {
            $metadataLog['id'] = $log['id'];
            $metadataLog['opt_type'] = 2;
            $metadataLog['update_time'] = date('Y-m-d H:i:s');
            $this->update('metadataLog', $metadataLog, false);
        }
        $this->success('撤回成功', url('admin/metadata/index'));
    }

    public function importLogIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['batchNo'])) {
            $where['batch_no'] = ['like', "%" . $param['batchNo'] . "%"];
        }
        $list = db('resource_metadata_log')->alias('rml')
            ->join('resource_metadata_type rmt', 'rml.type_id = rmt.id', 'left')
            ->field('rml.id as id, rml.batch_no,rml.type_id, rmt.name as typeName, rml.repeat_num, rml.fail_num, rml.success_num, rml.opt_type, rml.create_time, rml.update_time')
            ->where($where)
            ->order('create_time desc')
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('importLog', ['list' => $list]);
    }

    function getBatchNo() {
        $today = date('Ymd');
        $where['batch_no'] = ['like', "%" . $today . "%"];
        $data = model('metadataLog')
            ->where($where)
            ->select();
        $num = sizeof($data) + 1;
        if($num < 10) {
            $prefix = '000' . $num;
        } else if($num < 100) {
            $prefix = '00' . $num;
        } else if($num < 1000) {
            $prefix = '0' . $num;
        } else {
            $prefix = $num;
        }
        return $today . $prefix;
    }

    /**
     * 中文论文
     * @param array $excel_array
     */
    function getDbDataByType1(array $excel_array, $metaTypeId)
    {
        $repeatNum = 0;
        $failNUm = 0;
        $successNum = 0;
        $batchNo = $this->getBatchNo();
        foreach ($excel_array as $k => $v) {
            $dbData['id'] = $v['0'];
            $dbData['kid'] = $v['1'];
            $dbData['autoDelete'] = $v['2'];
            $dbData['unionId'] = $v['3'];
            $dbData['candidateId'] = $v['4'];
            $dbData['resourceIds'] = $v['5'];
            if ($v['6'] == null || $v['6'] == '' || empty($v['6'])) {
                $failNUm++;
                continue;
            }
            $where = array();
            $where['title'] = $v['6'];
            $where['resourceCode'] = $v['20'];
            $data = model('collection')
                ->where($where)
                ->find();
            if (!empty($data)) {
                $repeatNum++;
                continue;
            }
            $dbData['title'] = $v['6'];
            $dbData['titleAlt'] = $v['7'];
            $dbData['creator'] = $v['8'];
            $dbData['creatorAlt'] = $v['9'];
            $dbData['creatorAll'] = $v['10'];
            $dbData['keyword'] = $v['11'];
            $dbData['keywordAlt'] = $v['12'];
            $dbData['institute'] = $v['13'];
            $dbData['instituteAll'] = $v['14'];
            $dbData['year'] = $v['15'];
            $dbData['subject'] = $v['16'];
            $dbData['province'] = $v['17'];
            $dbData['city'] = $v['18'];
            $dbData['language'] = $v['19'];
            $dbData['resourceCode'] = $v['20'];
            $dbData['page'] = $v['21'];
            $dbData['doi'] = $v['22'];
            $dbData['issue'] = $v['23'];
            $dbData['creatorId'] = $v['24'];
            $dbData['url'] = $v['25'];
            $dbData['isCore'] = $v['26'];
            $dbData['conferenceName'] = $v['27'];
            $dbData['address'] = $v['28'];
            $dbData['issn'] = $v['29'];
            $dbData['isbn'] = $v['30'];
            $dbData['tutor'] = $v['31'];
            $dbData['degree'] = $v['32'];
            $dbData['publisher'] = $v['33'];
            $dbData['patentNo'] = $v['34'];
            $dbData['owner'] = $v['35'];
            $dbData['applicationNo'] = $v['36'];
            $dbData['type'] = $v['37'];
            if ($v['38'] == null || $v['38'] == "-") {
                $v['38'] = '1970-01-01 08:00:00';
            }
            $dbData['applicationDate'] = $v['38'];
            if ($v['39'] == null || $v['39'] == "-") {
                $v['39'] = '1970-01-01 08:00:00';
            }
            $dbData['publicationDate'] = $v['39'];
            $dbData['volume'] = $v['40'];
            $dbData['source'] = $v['41'];
            $dbData['journal'] = $v['42'];
            $dbData['domain'] = $v['43'];
            $dbData['languageStandard'] = $v['44'];
            $dbData['priority'] = $v['45'];
            $dbData['creatorStandard'] = $v['46'];
            $dbData['projectNo'] = $v['47'];
            $dbData['projectFund'] = $v['48'];
            $dbData['projectOrigin'] = $v['49'];
            $dbData['projectReward'] = $v['50'];
            $dbData['instituteStandard'] = $v['51'];
            $dbData['resourceIds'] = $v['52'];
            $dbData['teamMemberName'] = $v['53'];
            $dbData['teamMemberKId'] = $v['54'];
            $dbData['teamMemberWeight'] = $v['55'];
            $dbData['activityCategory'] = $v['56'];
            if ($v['57'] == null || $v['57'] == "-") {
                $v['57'] = '1970-01-01 08:00:00';
            }
            $dbData['activityDate'] = $v['57'];
            $dbData['activityBody'] = $v['58'];
            $dbData['filePath'] = $v['59'];
            $dbData['isSystem'] = $v['60'];
            $dbData['isValid'] = $v['61'];
            $dbData['isConfirmed'] = $v['62'];
            $dbData['isDeleted'] = $v['63'];
            if ($v['64'] == null || $v['64'] == "-") {
                $v['64'] = '1970-01-01 08:00:00';
            }
            $dbData['operateTime'] = $v['64'];
            $dbData['create_time'] = date('Y-m-d H:i:s');

            $dbData['batch_no'] = $batchNo;
            $res = db::name('resource_collection_temporary')->insert($dbData,true);
            if ($res > 0) {
                $successNum++;
            } else {
                $failNUm++;
            }
        }
        return $this->extracted($metaTypeId, $batchNo, $repeatNum, $failNUm, $successNum);
    }

    /**
     * 项目纵向
     * @param $excel_array
     * @param array $dbData
     * @return array
     */
    public function getDbDataByType2($excel_array, $metaTypeId)
    {
        $repeatNum = 0;
        $failNUm = 0;
        $successNum = 0;
        $batchNo = $this->getBatchNo();
        foreach ($excel_array as $k => $v) {
            $dbData['schoolNo'] = $v['0'];
            $dbData['feebleNo'] = $v['1'];
            $dbData['longitudinalSupportingFunds'] = $v['2'];
            $dbData['scientificResearchIncentiveFunds'] = $v['3'];
            if ($v['4'] == null || $v['4'] == '' || empty($v['4'])) {
                $failNUm++;
                continue;
            }
            $where = array();
            $where['title'] = $v['4'];
            $where['resourceCode'] = 'O';
            $data = model('collection')
                ->where($where)
                ->find();
            if (!empty($data)) {
                $repeatNum++;
                continue;
            }
            $dbData['title'] = $v['4'];
            $dbData['projectName'] = $v['4'];
            $dbData['subItem'] = $v['5'];
            $dbData['principal'] = $v['6'];
            $dbData['headOfFinancialJobNumber'] = $v['7'];
            $dbData['participant'] = $v['8'];
            $dbData['undertaking'] = $v['9'];
            $dbData['co'] = $v['10'];
            $dbData['formOfCooperation'] = $v['11'];
            $dbData['category'] = $v['12'];
            $dbData['projectLevel'] = $v['13'];
            $dbData['projectOrigin'] = $v['14'];
            $dbData['projectClassification'] = $v['15'];
            $dbData['sourceNumber'] = $v['16'];
            $dbData['projectNature'] = $v['17'];
            $dbData['subjectClassification'] = $v['18'];
            $dbData['projectStatus'] = $v['19'];
            $dbData['totalAmount'] = $v['20'];
            $dbData['totalFunding'] = $v['21'];
            $dbData['accumulate'] = $v['22'];
            if ($v['23'] == null || $v['23'] == "-") {
                $v['23'] = '1970-01-01 08:00:00';
            }
            $dbData['detectionTime'] = $v['23'];
            if ($v['24'] == null || $v['24'] == "-") {
                $v['24'] = '1970-01-01 08:00:00';
            }
            $dbData['startDate'] = $v['24'];
            if ($v['25'] == null || $v['25'] == "-") {
                $v['25'] = '1970-01-01 08:00:00';
            }
            $dbData['endDate'] = $v['25'];
            $dbData['organizeForm'] = $v['26'];
            $dbData['serviceNationalEconomicIndustry'] = $v['27'];
            $dbData['socialEconomicGoal'] = $v['28'];
            $dbData['activityClassification'] = $v['29'];
            $dbData['remark'] = $v['30'];

            $dbData['id'] = getNewUuid();
            $dbData['resourceCode'] = 'O';
            $dbData['create_time'] = date('Y-m-d H:i:s');

            $dbData['batch_no'] = $batchNo;
            $res = db::name('resource_collection_temporary')->insert($dbData,true);
            if ($res > 0) {
                $successNum++;
            } else {
                $failNUm++;
            }
        }
        return $this->extracted($metaTypeId, $batchNo, $repeatNum, $failNUm, $successNum);
    }

    /**
     * 项目横向
     * @param $excel_array
     * @param array $dbData
     * @return array
     */
    public function getDbDataByType3($excel_array, $metaTypeId)
    {
        $repeatNum = 0;
        $failNUm = 0;
        $successNum = 0;
        $batchNo = $this->getBatchNo();
        foreach ($excel_array as $k => $v) {
            if ($v['0'] == null || $v['0'] == '' || empty($v['0'])) {
                $failNUm++;
                continue;
            }
            $where = array();
            $where['title'] = $v['0'];
            $where['resourceCode'] = 'O';
            $data = model('collection')
                ->where($where)
                ->find();
            if (!empty($data)) {
                $repeatNum++;
                continue;
            }
            $dbData['title'] = $v['0'];
            $dbData['projectName'] = $v['0'];
            $dbData['schoolNo'] = $v['1'];
            $dbData['projectBelongs'] = $v['2'];
            $dbData['category'] = $v['3'];
            $dbData['principal'] = $v['4'];
            $dbData['headOfFinancialJobNumber'] = $v['5'];
            $dbData['participant'] = $v['6'];
            $dbData['undertaking'] = $v['7'];
            $dbData['requester'] = $v['8'];
            $dbData['commission'] = $v['9'];
            $dbData['contractAmount'] = $v['10'];
            $dbData['accumulate'] = $v['11'];
            $dbData['total'] = $v['12'];
            $dbData['contractNature'] = $v['13'];
            $dbData['entrustedUnitContact'] = $v['14'];
            $dbData['entrustedUnitTelephone'] = $v['15'];
            if ($v['16'] == null || $v['16'] == "-") {
                $v['16'] = '1970-01-01 08:00:00';
            }
            $dbData['signDate'] = $v['16'];
            $dbData['startDate'] = $v['17'];
            $dbData['endDate'] = $v['18'];
            $dbData['subject'] = $v['19'];
            $dbData['projectStatus'] = $v['20'];
            $dbData['departing'] = $v['21'];
            $dbData['externalProject'] = $v['22'];
            $dbData['externalInformation'] = $v['23'];
            $dbData['externalAmount'] = $v['24'];
            $dbData['subItem'] = $v['25'];
            $dbData['whetherYouTax'] = $v['26'];
            $dbData['dutyFreeContractRegistration'] = $v['27'];
            $dbData['dutyFreeAmount'] = $v['28'];
            $dbData['feebleNo'] = $v['29'];
            $dbData['scientificResearchIncentiveFunds'] = $v['30'];
            $dbData['fundSourceType'] = $v['31'];
            $dbData['serviceNationalEconomicIndustry'] = $v['32'];
            $dbData['socialEconomicGoal'] = $v['33'];

            $dbData['id'] = getNewUuid();
            $dbData['resourceCode'] = 'O';
            $dbData['create_time'] = date('Y-m-d H:i:s');

            $dbData['batch_no'] = $batchNo;
            $res = db::name('resource_collection_temporary')->insert($dbData,true);
            if ($res > 0) {
                $successNum++;
            } else {
                $failNUm++;
            }
        }
        return $this->extracted($metaTypeId, $batchNo, $repeatNum, $failNUm, $successNum);
    }

    /**
     * 专利
     * @param $excel_array
     * @param array $dbData
     * @return array
     */
    public function getDbDataByType4($excel_array, $metaTypeId)
    {
        $repeatNum = 0;
        $failNUm = 0;
        $successNum = 0;
        $batchNo = $this->getBatchNo();
        foreach ($excel_array as $k => $v) {
            if ($v['2'] == null || $v['2'] == '' || empty($v['2'])) {
                $failNUm++;
                continue;
            }
            $where = array();
            $where['title'] = $v['2'];
            $where['resourceCode'] = 'P';
            $data = model('collection')
                ->where($where)
                ->find();
            if (!empty($data)) {
                $repeatNum++;
                continue;
            }
            $dbData['title'] = $v['2'];
            $dbData['patentCategory'] = $v['0'];
            $dbData['schoolNo'] = $v['1'];
            $dbData['patentName'] = $v['2'];
            $dbData['applicationNo'] = $v['3'];
            if ($v['4'] == null || $v['4'] == "-") {
                $v['4'] = '1970-01-01 00:00:00';
            }
            $dbData['applicationDate'] = $v['4'];
            $dbData['firstInventory'] = $v['5'];
            $dbData['firstInventoryAcademy'] = $v['6'];
            $dbData['allInventors'] = $v['7'];
            $dbData['applicant'] = $v['8'];
            if ($v['9'] == null || $v['9'] == "-") {
                $v['9'] = '1970-01-01 00:00:00';
            }
            $dbData['authorizationTime'] = $v['9'];
            $dbData['legalState'] = $v['10'];
            $dbData['agency'] = $v['11'];
            $dbData['patentNo'] = $v['12'];

            $dbData['id'] = getNewUuid();
            $dbData['resourceCode'] = 'P';
            $dbData['create_time'] = date('Y-m-d H:i:s');

            $dbData['batch_no'] = $batchNo;
            $res = db::name('resource_collection_temporary')->insert($dbData,true);
            if ($res > 0) {
                $successNum++;
            } else {
                $failNUm++;
            }
        }
        return $this->extracted($metaTypeId, $batchNo, $repeatNum, $failNUm, $successNum);
    }

    /**
     * SCI/EI/图书
     * @param $excel_array
     * @param array $dbData
     * @return array
     */
    public function getDbDataByType5($excel_array, $metaTypeId)
    {
        $repeatNum = 0;
        $failNUm = 0;
        $successNum = 0;
        $batchNo = $this->getBatchNo();
        foreach ($excel_array as $k => $v) {
            $dbData['id'] = $v['0'];
            $dbData['resourceIds'] = $v['1'];
            if ($v['2'] == null || $v['2'] == '' || empty($v['2'])) {
                $failNUm++;
                continue;
            }
            $where = array();
            $where['title'] = $v['2'];
            $where['resourceCode'] = $v['8'];
            $data = model('collection')
                ->where($where)
                ->find();
            if (!empty($data)) {
                $repeatNum++;
                continue;
            }
            $dbData['title'] = $v['2'];
            $dbData['creator'] = $v['3'];
            $dbData['creatorAll'] = $v['4'];
            $dbData['keyword'] = $v['5'];
            $dbData['year'] = $v['6'];
            $dbData['subject'] = $v['7'];
            $dbData['resourceCode'] = $v['8'];
            $dbData['page'] = $v['9'];
            $dbData['doi'] = $v['10'];
            $dbData['issue'] = $v['11'];
            $dbData['url'] = $v['12'];
            $dbData['isCore'] = $v['13'];
            $dbData['conferenceName'] = $v['14'];
            $dbData['issn'] = $v['15'];
            $dbData['publisher'] = $v['16'];
            $dbData['patentNo'] = $v['17'];
            $dbData['applicationNo'] = $v['18'];
            $dbData['type'] = $v['19'];
            if ($v['20'] == null || $v['20'] == "-") {
                $v['20'] = '1970-01-01 08:00:00';
            }
            $dbData['applicationDate'] = $v['20'];
            if ($v['21'] == null || $v['21'] == "-") {
                $v['21'] = '1970-01-01 08:00:00';
            }
            $dbData['publicationDate'] = $v['21'];
            $dbData['volume'] = $v['22'];
            $dbData['source'] = $v['23'];
            $dbData['journal'] = $v['24'];
            $dbData['domain'] = $v['25'];
            $dbData['languageStandard'] = $v['26'];
            $dbData['priority'] = $v['27'];
            $dbData['creatorStandard'] = $v['28'];
            $dbData['projectNo'] = $v['29'];
            $dbData['projectOrigin'] = $v['30'];
            $dbData['projectReward'] = $v['31'];
            $dbData['instituteStandard'] = $v['32'];
            $dbData['clc'] = $v['33'];
            $dbData['journalId'] = $v['34'];
            $dbData['isSystem'] = $v['35'];
            $dbData['isValid'] = $v['36'];
            $dbData['confirmedExperts'] = $v['37'];
            $dbData['candidateResources'] = $v['38'];
            $dbData['isDeleted'] = $v['39'];
            if ($v['40'] == null || $v['40'] == "-") {
                $v['40'] = '1970-01-01 08:00:00';
            }
            $dbData['operateTime'] = $v['40'];
            $dbData['candidateExperts'] = $v['41'];
            $dbData['create_time'] = date('Y-m-d H:i:s');

            $dbData['batch_no'] = $batchNo;
            $res = db::name('resource_collection_temporary')->insert($dbData,true);
            if ($res > 0) {
                $successNum++;
            } else {
                $failNUm++;
            }
        }
        return $this->extracted($metaTypeId, $batchNo, $repeatNum, $failNUm, $successNum);
    }

    /**
     * 成果奖励
     * @param $excel_array
     * @return array
     */
    public function getDbDataByType8($excel_array, $metaTypeId)
    {
        $repeatNum = 0;
        $failNUm = 0;
        $successNum = 0;
        $batchNo = $this->getBatchNo();
        foreach ($excel_array as $v) {
            $title = $v['5'];
            if ($title == null || $title == '' || empty($title)) {
                $failNUm++;
                continue;
            }
            $where = array();
            $where['title'] = $title;
            $where['resourceCode'] = 'A';
            $data = model('collection')
                ->where($where)
                ->find();
            if (!empty($data)) {
                $repeatNum++;
                continue;
            }
            $dbData = array();
            $dbData['title'] = $title;
            $dbData['schoolNo'] = $v['0'];
            $dbData['achievement'] = $v['1'];
            $dbData['firstCompletionPerson'] = $v['2'];
            $dbData['allCompletedPeople'] = $v['3'];
            $dbData['allCompletedUnits'] = $v['4'];
            $dbData['achievementName'] = $title;
            $dbData['unitRankings'] = $v['6'];
            $dbData['locic'] = $v['7'];
            $dbData['awardType'] = $v['8'];
            $dbData['winningAward'] = $v['9'];
            $dbData['awardWinningLevel'] = $v['10'];
            $dbData['awardProcess'] = $v['11'];
            if (!empty($v['12']) && strlen($v['12']) > 4) {
                $v['12'] = substr($v['12'],0,4);
            }
            $dbData['awardWinningYear'] = $v['12'];
            if (!empty($v['13']) && strlen($v['13']) > 4) {
                $v['13'] = substr($v['13'],0,4);
            }
            $dbData['rewardYear'] = $v['13'];
            $dbData['subjectClassification'] = $v['14'];
            $dbData['rewardQuota'] = $v['15'];

            $dbData['id'] = getNewUuid();
            $dbData['resourceCode'] = 'A';
            $dbData['create_time'] = date('Y-m-d H:i:s');

            $dbData['batch_no'] = $batchNo;
            $res = db::name('resource_collection_temporary')->insert($dbData,true);
            if ($res > 0) {
                $successNum++;
            } else {
                $failNUm++;
            }
        }
        return $this->extracted($metaTypeId, $batchNo, $repeatNum, $failNUm, $successNum);
    }

    /**
     * 添加元数据导入日志
     * @param $batchNo // 批次号
     * @param $repeatNum    // 重复数量
     * @param $failNUm  // 失败数量
     * @param $successNum   // 成功数量
     * @return array
     */
    public function extracted($metaTypeId, $batchNo, $repeatNum, $failNUm, $successNum)
    {
        $metadataLog = array();
        $metadataLog['batch_no'] = $batchNo;
        $metadataLog['type_id'] = $metaTypeId;
        $metadataLog['repeat_num'] = $repeatNum;
        $metadataLog['fail_num'] = $failNUm;
        $metadataLog['success_num'] = $successNum;
        $metadataLog['create_time'] = date('Y-m-d H:i:s');
        $metadataLog['update_time'] = date('Y-m-d H:i:s');
        $this->insert('metadataLog', $metadataLog, false);
        return $metadataLog;
    }

}
